package com.example.expandablelistview

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.google.android.material.bottomappbar.BottomAppBar
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.navigation.NavigationView

class BtmAppBarBasic : AppCompatActivity() {

    lateinit var btmSheetBehavior: BottomSheetBehavior<View>
    var context: Context = this@BtmAppBarBasic

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.btm_appbar_basic)
        initViews()
    }

    private fun initViews() {
        supportActionBar?.let { tb ->
            tb.title = "Basic"
            tb.setHomeButtonEnabled(true)
            tb.setDisplayHomeAsUpEnabled(true)
        }

        val bottomDrawer = findViewById<View>(R.id.btm_drawer_basic)
        val navigationView = findViewById<NavigationView>(R.id.nav_view_basic)
        val bottomAppBar = findViewById<BottomAppBar>(R.id.btm_app_bar_basic)
        navigationView.setNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.btm_search -> ToastUtil.showShort(context, "点击了搜索")
                R.id.btm_3d -> ToastUtil.showShort(context, " 点击了3D")
                R.id.btm_accelerator -> ToastUtil.showShort(context, "点击了加速")
                R.id.btm_dashboard -> ToastUtil.showShort(context, "点击了指示板")
            }
            true
        }

        btmSheetBehavior = BottomSheetBehavior.from(bottomDrawer)
        btmSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN

        with(bottomAppBar) {
            setNavigationIcon(R.drawable.ic_menu)
            replaceMenu(R.menu.menu_demo)

            setNavigationOnClickListener {
                btmSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
            }

            setOnMenuItemClickListener{ item ->
                when(item.itemId) {
                    R.id.btm_search -> ToastUtil.showShort(context, "点击了搜索")
                    R.id.btm_3d -> ToastUtil.showShort(context, " 点击了3D")
                    R.id.btm_accelerator -> ToastUtil.showShort(context, "点击了加速")
                    R.id.btm_dashboard -> ToastUtil.showShort(context, "点击了指示板")
                }
                false
            }
        }
    }

    override fun onBackPressed() {
        if (btmSheetBehavior.state != BottomSheetBehavior.STATE_HIDDEN) {
            btmSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
            return
        }
        super.onBackPressed()
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }

}